
import { _decorator, Component, Node, GFXColorAttachment, GFXDepthStencilAttachment, SpriteFrame, RenderTexture, Sprite, Camera } from 'cc';
const { ccclass, property, type } = _decorator;

@ccclass('Controller')
export class Controller extends Component {
    @property(Sprite)
    spriteFront: Sprite=null;
    @property(Camera)
    cameraFront: Camera=null;
    @property(Sprite)
    spriteBack: Sprite=null;
    @property(Camera)
    cameraBack: Camera=null;

    start() {
        this.refreshRenderTextureFront();
        this.refreshRenderTextureBack();
    }

    update(deltaTime: number) {
        // Your update function goes here.
    }
    aa(){
        
    }


    refreshRenderTextureFront(): void {
        const _colorAttachment = new GFXColorAttachment();
        const _depthStencilAttachment = new GFXDepthStencilAttachment();

        let renderTex = new RenderTexture();
        renderTex.reset({
            width: 600,
            height: 400,
            passInfo: {
                colorAttachments: [_colorAttachment],
                depthStencilAttachment: _depthStencilAttachment,
            }
        });


        const spriteframe = this.spriteFront.spriteFrame;
        const sp = new SpriteFrame();
        sp.reset({
            originalSize: spriteframe.originalSize,
            rect: spriteframe.rect,
            offset: spriteframe.offset,
            isRotate: spriteframe.rotated,
            borderTop: spriteframe.insetTop,
            borderLeft: spriteframe.insetLeft,
            borderBottom: spriteframe.insetBottom,
            borderRight: spriteframe.insetRight,
        });


        this.cameraFront.targetTexture = renderTex;
        sp.texture = renderTex;
        this.spriteFront.spriteFrame = sp;
    }
    refreshRenderTextureBack(): void {
        const _colorAttachment = new GFXColorAttachment();
        const _depthStencilAttachment = new GFXDepthStencilAttachment();

        let renderTex = new RenderTexture();
        renderTex.reset({
            width: 600,
            height: 400,
            passInfo: {
                colorAttachments: [_colorAttachment],
                depthStencilAttachment: _depthStencilAttachment,
            }
        });


        const spriteframe = this.spriteBack.spriteFrame;
        const sp = new SpriteFrame();
        sp.reset({
            originalSize: spriteframe.originalSize,
            rect: spriteframe.rect,
            offset: spriteframe.offset,
            isRotate: spriteframe.rotated,
            borderTop: spriteframe.insetTop,
            borderLeft: spriteframe.insetLeft,
            borderBottom: spriteframe.insetBottom,
            borderRight: spriteframe.insetRight,
        });


        this.cameraBack.targetTexture = renderTex;
        sp.texture = renderTex;
        this.spriteBack.spriteFrame = sp;
    }
}
